##Load in packages
library(foreign)
library(tidyverse)
library(broom)
library(MASS)
library(ggpubr)
library(jtools)
library(haven)
library(readxl)
library(plm)
library(betareg)
library(sandwich)
library(lmtest)
library(car)
source("http://www.michaelperess.com/r0.r") ##Provides Easy Functions For Durbin_watson and Breusch-Pagan Tests

##Load in the Data From Your Own Repository
FP <- read.csv("C:/Users/REPOSITORY/Downloads/TroopDeploymentData_Body.csv")
View(FP)
attach(FP)
attach(DA)

##Descriptive Statistics
summary(FP)
sapply(FP, sd, na.rm=TRUE)
sapply(FP, var, na.rm=TRUE)

##Get some Descriptive Statistics Graphs
plt<-ggplot(data=FP,mapping=aes(Year,Overseas))+geom_line()+xlab("Year")+ylab("Ratio of Troops Overseas")
plt
plt1<-ggplot(data=FP,mapping=aes(Year,TroopUse))+geom_line()+xlab("Year")+ylab("Overseas US Troop Usage")
plt1
Figureplt<-ggarrange(plt,plt1,ncol=2,nrow=1)
Figureplt
plt2<-ggplot(data=FP,mapping=aes(Year,Support))+geom_line()+xlab("Year")+ylab("Level of Presidential Support in Congress")
plt2
plt3<-ggplot(data=FP,mapping=aes(Year,Polarization))+geom_line()+xlab("Year")+ylab("Level of Polarization in Congress")
plt3

##Polarization and Proportion of Moderate Correlation
cor.test(FP$Polarization,FP$Moderate)
cor.test(FP$Polarization,FP$Support)
cor.test(FP$Support,FP$Co_Part)

##OLS Regressions on US_Ratio and Controls
Troop1<-lm(US_Ratio~Divided+Term+GDP_Growth+Unemploy+Defense+
        War_Length+Conflicts+ColdWar+Year, FP)
coeftest(Troop1, vcov=vcovHAC(Troop1,type="HC1"))
nobs(Troop1)
Troop2<-lm(US_Ratio~Polarization+Term+GDP_Growth+Unemploy+Defense+
         War_Length+Conflicts+ColdWar+Year, FP)
coeftest(Troop2, vcov=vcovHAC(Troop2,type="HC1"))
nobs(Troop2)
Troop3<-lm(US_Ratio~Divided+Polarization+Term+
         GDP_Growth+Unemploy+Defense+War_Length+Conflicts+ColdWar+Year, FP)
coeftest(Troop3, vcov=vcovHAC(Troop3,type="HC1"))
nobs(Troop3)
##Beta Regressions on US_Ratio and Controls
Troop4<-betareg(US_Ratio~Divided+Term+GDP_Growth+Unemploy+Defense+
              War_Length+Conflicts+ColdWar+Year,FP)
coeftest(Troop4, vcov=vcovHAC(Troop4,type="HC1"))
nobs(Troop4)
Troop5<-betareg(US_Ratio~Polarization+Term+GDP_Growth+Unemploy+Defense+
              War_Length+Conflicts+ColdWar+Year,FP)
coeftest(Troop5, vcov=vcovHAC(Troop5,type="HC1"))
nobs(Troop5)
Troop6<-betareg(US_Ratio~Divided+Polarization+Term+GDP_Growth+Unemploy+Defense+
              War_Length+Conflicts+ColdWar+Year,FP)
coeftest(Troop6, vcov=vcovHAC(Troop6,type="HC1"))
nobs(Troop6)

##Regression Diagnostics US_Ratio Troop Deployments
broom::glance(Troop1)
broom::glance(Troop2)
broom::glance(Troop3)
broom::glance(Troop4)
broom::glance(Troop5)
broom::glance(Troop6)


##Testing H1 and H2 Across Multiple Specifications - OLS
##VoteView Congressional Support
Troop7<-lm(US_Ratio~Support+Polarization+Term+
             GDP_Growth+Unemploy+Defense+War_Length+Conflicts+ColdWar+Year, FP)
coeftest(Troop7, vcov=vcovHAC(Troop7,type="HC1"))
nobs(Troop7)
##Number of Co-partisans
Troop8<-lm(US_Ratio~Co_Part+Polarization+Term+
             GDP_Growth+Unemploy+Defense+War_Length+Conflicts+ColdWar+Year, FP)
coeftest(Troop8, vcov=vcovHAC(Troop8,type="HC1"))
nobs(Troop8)
##Hearings
Troop9<-lm(US_Ratio~Hearings+Polarization+Term+
             GDP_Growth+Unemploy+Defense+War_Length+Conflicts+ColdWar+Year, FP)
coeftest(Troop9, vcov=vcovHAC(Troop9,type="HC1"))
nobs(Troop9)
##Testing H1 and H2 Across Multiple Specifications - Beta
##VoteView Congressional Support
Troop10<-betareg(US_Ratio~Support+Polarization+Term+GDP_Growth+Unemploy+Defense+
                   War_Length+Conflicts+ColdWar+Year,FP)
coeftest(Troop10, vcov=vcovHAC(Troop10,type="HC1"))
nobs(Troop10)
##Number of Co-Partisans
Troop11<-betareg(US_Ratio~Co_Part+Polarization+Term+GDP_Growth+Unemploy+Defense+
                   War_Length+Conflicts+ColdWar+Year,FP)
coeftest(Troop11, vcov=vcovHAC(Troop11,type="HC1"))
nobs(Troop11)
##Hearings
Troop12<-betareg(US_Ratio~Hearings+Polarization+Term+GDP_Growth+Unemploy+Defense+
                   War_Length+Conflicts+ColdWar+Year,FP)
coeftest(Troop12, vcov=vcovHAC(Troop12,type="HC1"))
nobs(Troop12)

##Regression Diagnostics
broom::glance(Troop7)
broom::glance(Troop8)
broom::glance(Troop9)
broom::glance(Troop10)
broom::glance(Troop11)
broom::glance(Troop12)


##Create Prediction Graphs for Polarization
plt4<-effect_plot(Troop3,pred=Polarization, interval=TRUE,partial.residuals = FALSE)+
  labs(x='Polarization',y='Ratio of Troops in the US')
plt4




##Load in the new Data set
DA <- read_excel("C:/Users/REPOSITORY/Downloads/DeploymentAppendix_Data.xlsx")
View(DA)
attach(DA)

##OLS Regressions for Troop Shifts with Country-Year Fixed Effects
#Ratio
C1<-lm(Troop_Ratio_D~Divided+Polarization+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+as.factor(ccode1)+as.factor(year), data=DA)
coeftest(C1, vcov=vcovCL(C1,cluster=DA$ccode1,type='HC1'))
nobs(C1)
#Raw
C2<-lm(Troops~Divided+Polarization+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+OverseasTroops+as.factor(ccode1)+as.factor(year), data=DA)
coeftest(C2, vcov=vcovCL(C2,cluster=DA$ccode1,type='HC1'))
nobs(C2)
#Logged
C3<-lm(lntroops100~Divided+Polarization+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+Log_Deployed+as.factor(ccode1)+as.factor(year), data=DA)
coeftest(C3, vcov=vcovCL(C3,cluster=DA$ccode1,type='HC1'))
nobs(C3)

##Checking for Heteroskedasticity 
lmtest::bptest(C1)
lmtest::bptest(C2)
lmtest::bptest(C3)
##Heteroskedasticity is present
##Test for Autocorrelation
dwtest(Troop_Ratio_D~Divided+Polarization+FoPo+War_Length+
         Dependence+MID+Combat+Democracy)

##VIF to show lack of Mutlicollinearity
##Rerun C1 but without Country-Year Fixed Effects - Code Breaks With Them
C1<-lm(Troop_Ratio_D~Divided+Polarization+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy, data=DA)
vif(C1)
##No Multicollinearity

##Regression Diagnostics
broom::glance(C1)
broom::glance(C2)
broom::glance(C3)





##Appendix Tests - Separate Congress by Chamber and Re-run Tests under different constraints
##Naive OLS Regressions - US Troop Ratios
Naive1<-lm(US_Ratio~Divided, FP)
coeftest(Naive1,vcov=vcovHAC(Naive1,type='HC1'))
nobs(Naive1)
Naive2<-lm(US_Ratio~Support, FP)
coeftest(Naive2,vcov=vcovHAC(Naive2,type='HC1'))
nobs(Naive2)
Naive3<-lm(US_Ratio~Hearings, FP)
coeftest(Naive3,vcov=vcovHAC(Naive3,type='HC1'))
nobs(Naive3)
Naive4<-lm(US_Ratio~Co_Part, FP)
coeftest(Naive4,vcov=vcovHAC(Naive4,type='HC1'))
nobs(Naive4)
Naive5<-lm(US_Ratio~Polarization, FP)
coeftest(Naive5,vcov=vcovHAC(Naive5,type='HC1'))
nobs(Naive5)
Naive6<-lm(US_Ratio~Divided+Support+Hearings+Co_Part+Polarization, FP)
coeftest(Naive6,vcov=vcovHAC(Naive6,type='HC1'))
nobs(Naive6)

##Regression Diagnostics
broom::glance(Naive1)
broom::glance(Naive2)
broom::glance(Naive3)
broom::glance(Naive4)
broom::glance(Naive5)
broom::glance(Naive6)



##House And Senate on Troop Deployments
HS<-lm(US_Ratio~SUP_S+SUP_H+Co_Part_S+Co_Part_H+Term+GDP_Growth+Unemploy+Defense+
         War_Length+Conflicts+ColdWar+Year, FP)
coeftest(HS, vcov=vcovHAC(HS,type="HC1"))
nobs(HS)
HS1<-lm(US_Ratio~POL_S+POL_H+Term+GDP_Growth+Unemploy+Defense+
          War_Length+Conflicts+ColdWar+Year, FP)
coeftest(HS1, vcov=vcovHAC(HS1,type="HC1"))
nobs(HS1)
HS2<-lm(US_Ratio~SUP_S+SUP_H+Co_Part_S+Co_Part_H+POL_S+POL_H+Term+GDP_Growth+Unemploy+
          Defense+War_Length+Conflicts+ColdWar+Year, FP)
coeftest(HS2, vcov=vcovHAC(HS2,type="HC1"))
nobs(HS2)
##Beta Regressions on US_Ratio and Controls
HS3<-betareg(US_Ratio~SUP_S+SUP_H+Co_Part_S+Co_Part_H+Term+GDP_Growth+Unemploy+Defense+
               War_Length+Conflicts+ColdWar+Year,FP)
coeftest(HS3, vcov=vcovHAC(HS3,type="HC1"))
nobs(HS3)
HS4<-betareg(US_Ratio~POL_S+POL_H+Term+GDP_Growth+Unemploy+Defense+
               War_Length+Conflicts+ColdWar+Year,FP)
coeftest(HS4, vcov=vcovHAC(HS4,type="HC1"))
nobs(HS4)
HS5<-betareg(US_Ratio~SUP_S+SUP_H+Co_Part_S+Co_Part_H+POL_S+POL_H+Term+GDP_Growth+Unemploy+
               Defense+War_Length+Conflicts+ColdWar+Year,FP)
coeftest(HS5, vcov=vcovHAC(HS5,type="HC1"))
nobs(HS5)

##Regression Diagnostics
broom::glance(HS)
broom::glance(HS1)
broom::glance(HS2)
broom::glance(HS3)
broom::glance(HS4)
broom::glance(HS5)

##Changing Troop Deployment Ratio Variable with Log of Variable
TLog<-lm(Log_US~Divided+Log_Total+Term+GDP_Growth+Unemploy+Defense+
         War_Length+Conflicts+ColdWar+Year, FP)
coeftest(TLog, vcov=vcovHAC(TLog,type="HC1"))
nobs(TLog)
TLog1<-lm(Log_US~Polarization+Log_Total+Term+GDP_Growth+Unemploy+Defense+
         War_Length+Conflicts+ColdWar+Year, FP)
coeftest(TLog1, vcov=vcovHAC(TLog1,type="HC1"))
nobs(TLog1)
TLog2<-lm(Log_US~Divided+Polarization+Log_Total+Term+GDP_Growth+Unemploy+Defense+
         War_Length+Conflicts+ColdWar+Year, FP)
coeftest(TLog2, vcov=vcovHAC(TLog2,type="HC1"))
nobs(TLog2)

##Regression Diagnostics
broom::glance(TLog)
broom::glance(TLog1)
broom::glance(TLog2)



##OLS Regressions for Troop Shifts - Changing Support Measures
#Ratio - Support
D1<-lm(Troop_Ratio_D~Support+Polarization+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+as.factor(ccode1)+as.factor(year), data=DA)
coeftest(D1, vcov=vcovCL(D1,cluster=DA$ccode1,type='HC1'))
nobs(D1)
#Raw - Support
D2<-lm(Troops~Support+Polarization+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+OverseasTroops+as.factor(ccode1)+as.factor(year), data=DA)
coeftest(D2, vcov=vcovCL(D2,cluster=DA$ccode1,type='HC1'))
nobs(D2)
#Logged - Support
D3<-lm(lntroops100~Support+Polarization+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+Log_Deployed+as.factor(ccode1)+as.factor(year), data=DA)
coeftest(D3, vcov=vcovCL(D3,cluster=DA$ccode1,type='HC1'))
nobs(D3)

#Ratio - Hearings
D4<-lm(Troop_Ratio_D~Hearings+Polarization+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+as.factor(ccode1)+as.factor(year), data=DA)
coeftest(D4, vcov=vcovCL(D4,cluster=DA$ccode1,type='HC1'))
nobs(D4)
#Raw - Hearings 
D5<-lm(Troops~Hearings+Polarization+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+OverseasTroops+as.factor(ccode1)+as.factor(year), data=DA)
coeftest(D5, vcov=vcovCL(D5,cluster=DA$ccode1,type='HC1'))
nobs(D5)
#Logged - Hearings
D6<-lm(lntroops100~Hearings+Polarization+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+Log_Deployed+as.factor(ccode1)+as.factor(year), data=DA)
coeftest(D6, vcov=vcovCL(D6,cluster=DA$ccode1,type='HC1'))
nobs(D6)

#Ratio - Co-Partisan
D7<-lm(Troop_Ratio_D~CoPart+Polarization+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+as.factor(ccode1)+as.factor(year), data=DA)
coeftest(D7, vcov=vcovCL(D7,cluster=DA$ccode1,type='HC1'))
nobs(D7)
#Raw - Co-Partisan
D8<-lm(Troops~CoPart+Polarization+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+OverseasTroops+as.factor(ccode1)+as.factor(year), data=DA)
coeftest(D8, vcov=vcovCL(D8,cluster=DA$ccode1,type='HC1'))
nobs(D8)
#Logged - Co-Partisan
D9<-lm(lntroops100~CoPart+Polarization+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+Log_Deployed+as.factor(ccode1)+as.factor(year), data=DA)
coeftest(D9, vcov=vcovCL(D9,cluster=DA$ccode1,type='HC1'))
nobs(D9)

##Regression Diagnostics
broom::glance(D1)
broom::glance(D2)
broom::glance(D3)
broom::glance(D4)
broom::glance(D5)
broom::glance(D6)
broom::glance(D7)
broom::glance(D8)
broom::glance(D9)

##Measuring Based on Overseas Deployments - Log of Troop w/ Separating Congress
##OLS w/ Country-Year Fixed Effects w/ Cluster S.E.
##Support Ratio
P<-lm(Troop_Ratio_D~SUP_S+SUP_H+POL_S+POL_H+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+as.factor(ccode1)+as.factor(year), data=DA)
coeftest(P,vcov=vcovCL(P,cluster=DA$ccode1, type='HC1'))
nobs(P)
##Support Raw
P1<-lm(Troops~SUP_S+SUP_H+POL_S+POL_H+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+OverseasTroops+as.factor(ccode1)+as.factor(year), data=DA)
coeftest(P1,vcov=vcovCL(P1,cluster=DA$ccode1, type='HC1'))
nobs(P1)
##Support Logged
P2<-lm(lntroops100~SUP_S+SUP_H+POL_S+POL_H+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+Log_Deployed+as.factor(ccode1)+as.factor(year), data=DA)
coeftest(P2,vcov=vcovCL(P2,cluster=DA$ccode1, type='HC1'))
nobs(P2)

##Co-Partisan Ratio
P3<-lm(Troop_Ratio_D~CoPart_S+CoPart_H+POL_S+POL_H+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+as.factor(ccode1)+as.factor(year), data=DA)
coeftest(P3,vcov=vcovCL(P3,cluster=DA$ccode1, type='HC1'))
nobs(P3)
##Co-Partisan Raw
P4<-lm(Troops~CoPart_S+CoPart_H+POL_S+POL_H+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+OverseasTroops+as.factor(ccode1)+as.factor(year), data=DA)
coeftest(P4,vcov=vcovCL(P4,cluster=DA$ccode1, type='HC1'))
nobs(P4)
##Co-Partisan Logged
P5<-lm(lntroops100~CoPart_S+CoPart_H+POL_S+POL_H+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+Log_Deployed+as.factor(ccode1)+as.factor(year), data=DA)
coeftest(P5,vcov=vcovCL(P5,cluster=DA$ccode1, type='HC1'))
nobs(P5)


##Get Regression Diagnostics
broom::glance(P)
broom::glance(P1)
broom::glance(P2)
broom::glance(P3)
broom::glance(P4)
broom::glance(P5)

##Regressions Differentiating Combat Zones from Non-Combat Zones
##Country-Year Fixed Effects w/ Robust Standard Errors - Non-Combat Zones
#Ratio
R<-lm(Troop_Ratio_D~Divided+Polarization+FoPo+ColdWar+
         Dependence+MID+War_Length+Democracy+as.factor(ccode1)+as.factor(year), data=subset(DA,Combat==0))
coeftest(R,vcov=vcovHC(R, type='HC1'))
nobs(R)
#Raw
R1<-lm(Troops~Divided+Polarization+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+OverseasTroops+as.factor(ccode1)+as.factor(year), data=subset(DA,Combat==0))
coeftest(R1,vcov=vcovHC(R1, type='HC1'))
nobs(R1)
#Logged
R2<-lm(lntroops100~Divided+Polarization+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+Log_Deployed+as.factor(ccode1)+as.factor(year), data=subset(DA,Combat==0))
coeftest(R2,vcov=vcovHC(R2, type='HC1'))
nobs(R2)

##Country-Year Fixed Effects w/ Robust Standard Errors - Combat Zones
#Ratio
R3<-lm(Troop_Ratio_D~Divided+Polarization+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+as.factor(ccode1)+as.factor(year), data=subset(DA,Combat==1))
coeftest(R3,vcov=vcovHC(R3, type='HC1'))
nobs(R3)
##Raw
R4<-lm(Troops~Divided+Polarization+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+OverseasTroops+as.factor(ccode1)+as.factor(year), data=subset(DA,Combat==1))
coeftest(R4,vcov=vcovHC(R4, type='HC1'))
nobs(R4)
##Logged
R5<-lm(lntroops100~Divided+Polarization+FoPo+ColdWar+
         Dependence+MID+Combat+War_Length+Democracy+Log_Deployed+as.factor(ccode1)+as.factor(year), data=subset(DA,Combat==1))
coeftest(R5,vcov=vcovHC(R5, type='HC1'))
nobs(R5)

##Regression Diagnostics
broom::glance(R)
broom::glance(R1)
broom::glance(R2)
broom::glance(R3)
broom::glance(R4)
broom::glance(R5)